{--
FREE BEER LICENSE VERSION 1.02

The free beer license is a license to give free software to you and free 
beer (in)to the author(s).

Your rights are :

0. You can use this piece of software in anyway you like.

1. You can redistribute this piece of software in source form or in 
   compiled form. 

2. You can alter the source to your needs and redistribute the altered 
   source in source form or in compiled form.

However :

0. This program is provided without warranty of any kind. So, if it 
   breaks anything, for example itself, it is up to you. 

1. If you redistribute this piece of software, you are not allowed to 
   charge money for this piece of software itself.

2. If you redistribute this pieces of software in binary form, you must 
   supply the source code as well.

3. If you redistribute this software, modified or not, you must 
   redistribute it under this license and you must include the name of 
   the original author(s) and you must point out where the original 
   source can be obtained.

4. If you use this piece of software frequently, and you think it is 
   worth a couple of euros, you are not allowed to send the author 
   anything else than beer or means that provide facilities to get beer 
   into the author(s) (i.e. openers, glasses).
--}

{--
        Authors: 
        A. Grem 
        P. Kirstahler   
        B. Petri 
        B. Schubert
        P. Seitz

--}
module Main where

import Strategy
import Environment.Representation as R
import Environment.Game
import System.IO
import System.IO.Unsafe
-- <<<<<<< local
-- hiding import Data.Vector as V
import Control.Monad(return)
import Control.Concurrent

-- =======
-- import Control.Monad (liftM)
-- >>>>>>> other
import System.Environment ( getArgs )

main::IO()
main = do
        hPutStrLn stderr "Your are on your way to Destruction." 
        hPutStrLn stderr "Make your time !" 
        threadDelay 1000
        startingPlayer <- getArgs
        game <- return initGame
        strategy <- return firstMoves
        if ((startingPlayer == []) || ((head startingPlayer) == "0"))
                then do
                        move <- return (getNextAction strategy game)
                        game <- return (updateGame (fst move) True game)
                        writeServer (fst move)
                        mainLoop (snd move) game
                else do
                        mainLoop strategy game

-- | Main loop of the game, repeats until game is ended
mainLoop::Strategy -> Game -> IO()
mainLoop s g = do
   {-- Debugausgabe:
        hPutStrLn stderr "-----------------------------------------------------------------------------"
        hPutStrLn stderr "prop:"
        hPutStrLn stderr ("Slot 0:" ++ (show (getSlotI 0 (prop g))))
        hPutStrLn stderr ("Slot 1:" ++ (show (getSlotI 1 (prop g))))
        hPutStrLn stderr ("Slot 2:" ++ (show (getSlotI 2 (prop g))))
        hPutStrLn stderr ("Slot 3:" ++ (show (getSlotI 3 (prop g))))
        hPutStrLn stderr ("Slot 7:" ++ (show (getSlotI 7 (prop g))))
        hPutStrLn stderr ("Slot 8:" ++ (show (getSlotI 8 (prop g))))
        hPutStrLn stderr ("Slot 9:" ++ (show (getSlotI 9 (prop g))))
        hPutStrLn stderr ("Slot 10:" ++ (show (getSlotI 10 (prop g))))
        hPutStrLn stderr ("Slot 11:" ++ (show (getSlotI 11 (prop g))))
        hPutStrLn stderr ("Slot 76:" ++ (show (getSlotI 76 (prop g))))
        hPutStrLn stderr ("Slot 254:" ++ (show (getSlotI 254 (prop g))))
        hPutStrLn stderr "-----------------------------------------------------------------------------"
        hPutStrLn stderr "op:"
        hPutStrLn stderr ("Slot 0:" ++ (show (getSlotI 0 (op g))))
        hPutStrLn stderr "-----------------------------------------------------------------------------"
        hFlush stderr  --}
     --   hPutStrLn stderr ("Leben Slot 0:" ++ (show (getVitality (prop g) 0)))
        -- Get enemy action
        enemyAction <- readCard
        -- Update the game with the enemy action
        game <- return (updateGame enemyAction False g)
        -- Get the next move
--        hPutStrLn stderr "--------------------------------- Env"
--        hFlush stderr
        move <- return (getNextAction s game) 
--        hPutStrLn stderr "--------------------------------- Strat"
--        hPutStrLn stderr ("-------------" ++ (show  $fst move))
--        hFlush stderr
        game2 <- return (updateGame (fst move) True game)
        
       
        writeServer (fst move) -- send action to the server
        -- do this until the end of days, or victory
        mainLoop (snd move) game2

-- | Writes a certain Action in the console
writeServer::Action->IO()
writeServer a | action == 1 = do
                         putStr  $ "1\n" ++ (card2Name card) ++ "\n" ++ (show field)++"\n"
                         hFlush stdout
              | action == 2 = do
                         putStr $ "2\n" ++ (show field) ++ "\n" ++ (card2Name card) ++"\n"
                         hFlush stdout
              where action = getApp a
                    card = getCard a
                    field = getSlot a

-- | Reads the input from console and converts it into an IO Action Type
readCard:: IO Action
readCard = do
        applic <- getLine
        first <- getLine
        second <- getLine
        if (applic == "1")
                then do
                        return ((read applic :: Int), name2Card first, (read second ::Int))
                else do
                        return ((read applic :: Int), name2Card second, (read first ::Int))




